<?php
/**
 * 系统设置模型
 * User: ws
 * Date: 2018/7/23
 * Time: 16:39
 */

namespace app\admin\model;
use think\Model;

class Config extends Model
{
    function get_list($where = '',$order = 'config_id ASC',$page_size=15,$whith_page=0){
        if(!$order){ $order = 'config_id ASC';}
        if($whith_page == 0){
            $articles = $this->where($where)->order($order)->limit($page_size)->select();
            return $articles;
        }
        $articles = $this->where($where)->order($order)->paginate($page_size);
        if($whith_page == 1){
            return $articles;
        }
        $articles = $articles->toArray();
        return $articles;
    }


    function add($params,$resul){
        foreach($resul['data'] as $val){
            $temp[$val['name']] = $val['value'];
        }

        foreach ($params as $k=>$v){
            $newArr = array('name'=>$k,'value'=>trim($v));
            if(!isset($temp[$k])){
                $result = $this->isUpdate(false)->allowField(true)->save($newArr);
                if(!$result){
                    return false;
                }

            }elseif($v!=$temp[$k]){
                $result = $this->update(true)->allowField(true)->save($newArr,'name ='."'{$k}'");
                if(!$result){
                    return false;
                }
            }
        }

        return true;

    }

}